/*
 * Main.java
 *
 * Created on 6 March 2006, 11:51
 *
 */

package mac5855.app.lucene.demo;

import java.util.Iterator;

import mac5855.app.lucene.search.Indexer;
import mac5855.app.lucene.search.SearchEngine;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Hit;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Weight;

/**
 * 
 * @author John
 */
public class Main {

	/** Creates a new instance of Main */
	public Main() {

	}

	/**
	 * 
	 * @param args
	 *            the command line arguments
	 * 
	 */
	public static void main(String[] args) {

		try {
			// build a lucene index
			System.out.println("rebuildIndexes");
			 Indexer indexer = new Indexer();
			 indexer.rebuildIndexes();
			System.out.println("rebuildIndexes done");

			// perform search on "Notre Dame museum"
			// and retrieve the result
			System.out.println("performSearch");
			SearchEngine instance = new SearchEngine();

			Hits hits = instance.performSearch("voce");
			IndexSearcher indexSearcher = instance.getSearcher();
			Weight weight = instance.getWeight();

			System.out.println("Results found: " + hits.length());
			Iterator<Hit> iter = hits.iterator();
			while (iter.hasNext()) {
				Hit hit = iter.next();
				Document doc = hit.getDocument();
				System.out.println(doc.get("id"));
				System.out.println(doc.get("titulo"));
				System.out.println(doc.get("categoria"));
				System.out.println(" (" + hit.getScore() + ")");
				Explanation explanation = indexSearcher.explain(weight, hit.getId());
				System.out.println(explanation.toString());

			}
			System.out.println("performSearch done");
		} catch (Exception e) {
			System.out.println("Exception caught.\n");
		}
	}
}
